#include <iostream>
#include <chrono>
#include <ratio> 
#include <ctime>
#include <iomanip>
//template <intmax_t N, intmax_t D = 1> class ratio; 
//该模板用于实例化由分子和分母表示的有限有理数的类型。分子和分母被实现为intmax t类型的编译时常量。

int main(void)
{
        using namespace std::chrono;
    {
        /** time_point  表示时间点的time_point, 用来获取从它的clock的纪元开始所经过的duration 和当前的时间，可以做一些时间的比较和算术运算，可以和ctime库集合起来显示时间
         * */
        typedef duration<int, std::ratio<60*60*24>> days_type; //定义一天的duration
        time_point<system_clock, days_type> today = time_point_cast<days_type>(system_clock::now());

        std::cout << today.time_since_epoch().count() << " Days since epoch" << std::endl;
    }

    {
        system_clock::time_point now = system_clock::now();
        std::time_t last = system_clock::to_time_t(now - std::chrono::hours(24)); //time_point 与 duration 相加减，进行时间的跳转
        std::time_t next = system_clock::to_time_t(now + hours(24));

        std::cout << "One day ago, the time was " 
            << std::put_time(std::localtime(&last), "%F %T") << "\n"; //put_time (const struct tm* tmb, const charT* fmt); 插入 tmb 指向的时间和日期信息的表示形式，按照参数 fmt 的指定对其进行格式化。
        std::cout << "Next day, the time is "  
            << std::put_time(std::localtime(&next), "%F %T") << "\n";

    }

    {
        using std::chrono::system_clock;


        std::chrono::duration<int, std::ratio<60*60*24>> one_day(1);
        system_clock::time_point today = system_clock::now(); //Get current time
        system_clock::time_point tommorrow = today + one_day;

        std::time_t tt;

        tt = system_clock::to_time_t(today); //Convert from time_t
        std::cout << "today is : " << ctime(&tt); //char *ctime(const time_t *timep);

        tt = system_clock::to_time_t(tommorrow);
        std::cout << "tomorrow will be: " << ctime(&tt);
    }
    return 0; 
}
